package org.test.controller;

import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.test.service.BookService;
import org.test.service.OrdersService;
import org.test.vo.Book;
import org.test.vo.Orders;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;
import java.util.UUID;

@Controller  //注解处理器
@RequestMapping("/Book")
public class BookController {
    //注入服务层对象
    @Autowired
    BookService bookService;
    @Autowired
    OrdersService ordersService;

    //添加图书
    @RequestMapping(value = "/addBook",method = RequestMethod.POST)
    public String addBook(HttpServletRequest request, Book book,@RequestParam("pictureFile")MultipartFile pictureFile,Model model,String bookname, String booktype) throws Exception{
        //使用UUID给图片重命名，并去掉四个“-”
            String name = UUID.randomUUID().toString().replaceAll("-", "");
            //获取文件的扩展名
            String ext = FilenameUtils.getExtension(pictureFile.getOriginalFilename());
            //设置图片上传路径  "/images/book/"
            String url = request.getSession().getServletContext().getRealPath("/images/book/");
            System.out.println(url);
            //以绝对路径保存重名命后的图片
            pictureFile.transferTo(new File(url + "/" + name + "." + ext));
            //保存到数据库的图片路径
            String dataPath = name + "." + ext;
            book.setBookName(bookname);
            book.setBookType(booktype);
            book.setImage(dataPath);
            //调用方法
                Book addBook= bookService.addBook(book);
             if(addBook!=null){
                 model.addAttribute("message","恭喜添加图书成功");
                 return "public/message";
             }else {
                 model.addAttribute("message","失败");
                 return "public/message";
             }


    }

    //查询所有图书和订单，返回数据到管理员首页并跳转
    @RequestMapping(value = "/bookAll",method = RequestMethod.GET)
    public String bookAll(Model model) {
        //查询图书
        List<Book> bookList=bookService.getAllBooks();
        //存储数据
        model.addAttribute("books", bookList);
        System.out.println("====="+model);
        //查询订单
        List<Orders> ordersList = ordersService.getAllOrders();
        //存储数据
        model.addAttribute("orders",ordersList);
        System.out.println(""+model);
        return "admin/admin-index";
    }


    //查询所有图书，返回数据到用户首页，并跳转页面
    @RequestMapping(value = "/UserbookAll",method = RequestMethod.GET)
    public String UserbookAll(Model model) {
        //查询图书
        List<Book> bookList=bookService.getAllBooks();
        //存储数据
        model.addAttribute("books", bookList);
        System.out.println("====="+model);
        return "user/user-index";
    }


    //根据图书名查询书籍信息
    @RequestMapping("/searchLikeBookname")
    public String searchLikeBookname(Model model, String searchText){
        //查询
        List<Book> searchbookList = bookService.searchLikeBookname(searchText);
        //存储
        model.addAttribute("searchbookList",searchbookList);
        return "user/user-searchBook";

    }

    //根据图书id，查询图书信息
    @RequestMapping("/bookNews")
    public String bookNew(Model model, int bookid){
        //调用方法
        Book bookNew=bookService.getBookByBookid(bookid);
        model.addAttribute("bookNew",bookNew);
        return "public/bookNews";
    }

}
